package com.group.zsxm.service.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.group.zsxm.entity.XtMenu;
import com.group.zsxm.entity.Xtuser;
import com.group.zsxm.exception.BusException;
import com.sun.org.apache.bcel.internal.generic.Select;

@Service
public class SystemService extends BaseService{
	
	
	/**
	 * 获取所有用户
	 */
	public List getXtuserList(){
		List l = new ArrayList();
		String sql = " select * from xt_user where loginname!='admin'";
		l = this.getSimpleJdbcTemplate().queryForList(sql);
		return l;
	}
	/**
	 * 获取登入日期
	 * @return
	 */
	public String getLogindate(){
		String sql = " select CONVERT(varchar(100), GETDATE(), 23) as logindate ";
		return String.valueOf(this.getSimpleJdbcTemplate().queryForObject(sql,String.class));
	}
	/**
	 * 企业用户登录检查
	 * @param u
	 * @return
	 */
	public Xtuser doCheckLogin(Xtuser u){
		String sql_q = "";
		Xtuser xtuser = new Xtuser();
		sql_q = "select count(*) from dw_info where lower(dwdm)='"+u.getLoginname().toLowerCase()+"' and lower(isnull(dwpassword,'111111'))='"+u.getPassword().toLowerCase()+"'";
		if(this.getSimpleJdbcTemplate().queryForInt(sql_q) > 0){
			sql_q = "select dwid userid,dwdm loginname,dwmc cnname,CONVERT(varchar(100), GETDATE(), 23) as logindate,dwdm,isnull(isjfh,0) isjfh from dw_info where lower(dwdm)='"+u.getLoginname().toLowerCase()+"' and isnull(dwpassword,'111111')='"+u.getPassword()+"'";
			xtuser = this.getSimpleJdbcTemplate().queryForObject(sql_q,resultBeanMapper(Xtuser.class));
			sql_q = " select count(*) from dwsb a,dwsb_mx b where a.bz=1 and a.dm=b.dm and b.dwid='"+xtuser.getUserid()+"'";
			if(this.getSimpleJdbcTemplate().queryForInt(sql_q)>0){
				sql_q = " select a.dm,a.value from dwsb a,dwsb_mx b where a.bz=1 and a.dm=b.dm and b.dwid='"+xtuser.getUserid()+"'";
				Map m = new HashMap();
				m = this.getSimpleJdbcTemplate().queryForMap(sql_q);
				xtuser.setLoginbz(Integer.parseInt(String.valueOf(m.get("value"))));
				xtuser.setSbdm(String.valueOf(m.get("dm")));
			}else{
				xtuser.setLoginbz(0);
			}
		}else{
			throw new BusException("您输入的用户名或密码错误！");
		}
		return xtuser;
	}

	/**
	 * 获取工作员用户可操作菜单
	 * @param userid
	 * @return
	 */
	public List<XtMenu> getMenuList(String userid,Integer isjfh){
		List<XtMenu> menus = new ArrayList();
		String sql_q = "select count(*) from xt_menu_sb ";
		if( this.getSimpleJdbcTemplate().queryForInt(sql_q) > 0){
			if(isjfh == 1){
				sql_q = "select * from xt_menu_sb  order by menuid" ;
			}else{
				sql_q = "select * from xt_menu_sb where menuid not like '03%' order by menuid" ;
			}
			menus = this.getSimpleJdbcTemplate().query(sql_q, resultBeanMapper(XtMenu.class));
		}
		return menus;
	}
	
	
	/**
	 * 获取人才用户可操作菜单
	 * @param userid
	 * @return
	 */
	public List<XtMenu> getMenuListWithRc(String userid){
		List<XtMenu> menus = new ArrayList();
		String sql_q = " select a.* from rc_menu a  order by a.menuid ";
		menus = this.getSimpleJdbcTemplate().query(sql_q, resultBeanMapper(XtMenu.class));
		return menus;
	}
	
	/**
	 * 更新密码
	 */
	public int doUpdatePassWord(String dwid,Map<String, String> m){
		String sql = "";
		sql = " select count(*) from dw_info where lower(isnull(dwpassword,'111111'))='"+m.get("oldpassword").toLowerCase()+"' and dwid='"+dwid+"'";
		if(this.getSimpleJdbcTemplate().queryForInt(sql) > 0){
			sql = " update dw_info set dwpassword='"+m.get("newpassword").trim()+"' where dwid='"+dwid+"'";
			this.getSimpleJdbcTemplate().update(sql);
			sql = " update dw_info_sb set dwpassword='"+m.get("newpassword").trim()+"' where dwid='"+dwid+"'";
			this.getSimpleJdbcTemplate().update(sql);
		}else{
			throw new BusException("您的原始密码不正确，不能修改！");
		}
		return 1;
	}

}
